using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SpellsTable{
	
	public Dictionary<int, Dictionary<int, int>> spellTable = new Dictionary<int, Dictionary<int, int>>
	{
		{1, 	new Dictionary<int,int>{{0,3},{1,1},{2,-1},{3,-1},{4,-1},{5,-1},{6,-1},{7,-1},{8,-1},{9,-1}}},
		{2, 	new Dictionary<int,int>{{0,4},{1,2},{2,-1},{3,-1},{4,-1},{5,-1},{6,-1},{7,-1},{8,-1},{9,-1}}},
		{3, 	new Dictionary<int,int>{{0,4},{1,2},{2,1},{3,-1},{4,-1},{5,-1},{6,-1},{7,-1},{8,-1},{9,-1}}},
		{4, 	new Dictionary<int,int>{{0,4},{1,3},{2,2},{3,-1},{4,-1},{5,-1},{6,-1},{7,-1},{8,-1},{9,-1}}},
		{5, 	new Dictionary<int,int>{{0,4},{1,3},{2,2},{3,1},{4,-1},{5,-1},{6,-1},{7,-1},{8,-1},{9,-1}}},
		{6, 	new Dictionary<int,int>{{0,4},{1,3},{2,3},{3,2},{4,-1},{5,-1},{6,-1},{7,-1},{8,-1},{9,-1}}},
		{7, 	new Dictionary<int,int>{{0,4},{1,4},{2,3},{3,2},{4,1},{5,-1},{6,-1},{7,-1},{8,-1},{9,-1}}},
		{8, 	new Dictionary<int,int>{{0,4},{1,4},{2,3},{3,3},{4,2},{5,-1},{6,-1},{7,-1},{8,-1},{9,-1}}},
		{9, 	new Dictionary<int,int>{{0,4},{1,4},{2,4},{3,3},{4,2},{5,1},{6,-1},{7,-1},{8,-1},{9,-1}}},
		{10, 	new Dictionary<int,int>{{0,4},{1,4},{2,4},{3,3},{4,3},{5,2},{6,-1},{7,-1},{8,-1},{9,-1}}},
		{11, 	new Dictionary<int,int>{{0,4},{1,4},{2,4},{3,4},{4,3},{5,2},{6,1},{7,-1},{8,-1},{9,-1}}},
		{12, 	new Dictionary<int,int>{{0,4},{1,4},{2,4},{3,4},{4,3},{5,3},{6,2},{7,-1},{8,-1},{9,-1}}},
		{13, 	new Dictionary<int,int>{{0,4},{1,4},{2,4},{3,4},{4,4},{5,3},{6,2},{7,1},{8,-1},{9,-1}}},
		{14, 	new Dictionary<int,int>{{0,4},{1,4},{2,4},{3,4},{4,4},{5,3},{6,3},{7,2},{8,-1},{9,-1}}},
		{15, 	new Dictionary<int,int>{{0,4},{1,4},{2,4},{3,4},{4,4},{5,4},{6,3},{7,2},{8,1},{9,-1}}},
		{16, 	new Dictionary<int,int>{{0,4},{1,4},{2,4},{3,4},{4,4},{5,4},{6,3},{7,3},{8,2},{9,-1}}},
		{17, 	new Dictionary<int,int>{{0,4},{1,4},{2,4},{3,4},{4,4},{5,4},{6,4},{7,3},{8,2},{9,1}}},
		{18, 	new Dictionary<int,int>{{0,4},{1,4},{2,4},{3,4},{4,4},{5,4},{6,4},{7,3},{8,3},{9,2}}},
		{19, 	new Dictionary<int,int>{{0,4},{1,4},{2,4},{3,4},{4,4},{5,4},{6,4},{7,4},{8,3},{9,3}}},
		{20, 	new Dictionary<int,int>{{0,4},{1,4},{2,4},{3,4},{4,4},{5,4},{6,4},{7,4},{8,4},{9,4}}}
	};
	
	public int GetSpells(int level, int spellLevel)
	{
		if(spellTable.Count >= level)
			if(spellTable[level].Count >= spellLevel)
				return spellTable[level][spellLevel];
		return 0;
	}
	
	public SpellsTable(string json = null)
	{
		if(json != null)
		{
			spellTable = new Dictionary<int, Dictionary<int, int>>();
			
			Dictionary<string, object> tableObject = MiniJSON.Json.Deserialize(json) as Dictionary<string, object>;
			
			if(tableObject == null)
				return;
			
			foreach(var d in tableObject)
			{
				spellTable.Add(int.Parse(d.Key), new Dictionary<int, int>());
				foreach(var dd in d.Value as Dictionary<string, object>)
				{
					spellTable[int.Parse(d.Key)].Add(int.Parse(dd.Key), int.Parse(dd.Value as string));
				}
			}
		}
	}
	
	public string ToJSON()
	{
		return MiniJSON.Json.Serialize(spellTable);
	}
}
